<%
# Code Review plugin for Redmine
# Copyright (C) 2010  Haruyuki Iida
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
-%>
<div id="code_review_settings">
  <script type="text/javascript">
  function setAutoAssignSettingFormEnable(flag) {
    var form = $('cr_auto_assign_settings');
    if (flag) {
      new Effect.BlindDown(form);
    }
    else {
      new Effect.BlindUp(form);
    }
  }

  function setAutoAssignSettingFiltersEnable(flag) {
    var form = $('auto_assignment_filter_form');
    if (flag) {
      new Effect.BlindDown(form);
    }
    else {
      new Effect.BlindUp(form);
    }
  }

  function is_valid_expression(field) {
    if (field.value == '') {
      return false;
    }

    try {
      new RegExp(field.value);
    }
    catch (e) {
      alert(e);
      return false;
    }

    return true;
  }
</script>
  
  <%
  @code_review_setting = CodeReviewProjectSetting.find_or_create(@project)

  @old_reviews = CodeReview.find(:all,
    :conditions => ['issue_id is ? and old_parent_id is ? and project_id = ?', nil, nil, @project.id])

%>

  <% labelled_tabular_form_for :setting, @code_review_setting,
    :url => {:controller => 'code_review_settings',
    :action => 'update', :id => @project, :tab => 'code_review',
    :partial => 'code_review_settings/update',
    :setting_id => @code_review_setting.id}, :html => {:id => 'code_review_form'} do |f| %>
    <%= error_messages_for 'code_review_setting' %>
    <div class="box">
      <%= f.hidden_field :lock_version %>
      <b><%=h l(:select_tracker_for_code_reviews)%>:</b>
      <p><%= f.select :tracker_id, @project.trackers.collect {|t| [t.name, t.id]}, :required => true %></p>

      <b><%=h l(:select_tracker_for_code_review_assignment)%>:</b>
      <p><%= f.select :assignment_tracker_id, @project.trackers.collect {|t| [t.name, t.id]}, :required => true %></p>

      <p>
        <%= f.check_box :hide_code_review_tab %>
      </p>

      <p>
        <label><%=h l(:label_if_revision_has_issues)%>:</label>
        <%= f.radio_button(:auto_relation, CodeReviewProjectSetting::AUTORELATION_TYPE_RELATES) %>
        <%=h l(:label_review_issue_relates) %>

        <%= f.radio_button(:auto_relation, CodeReviewProjectSetting::AUTORELATION_TYPE_BLOCKS) %>
        <%=h l(:label_review_issue_blocks) %>

        <%= f.radio_button(:auto_relation, CodeReviewProjectSetting::AUTORELATION_TYPE_NONE) %>
        <%=h l(:label_review_issue_do_nothing) %>

      </p>
      <hr/>

      <p>
        <%
        @auto_assign = @code_review_setting.auto_assign_settings
        @auto_assign.subject = l(:code_review_requrest) if @auto_assign.subject.blank?
      -%>
        <label><%=h l(:label_auto_assign_settings)%></label>
        <%= check_box_tag "auto_assign[enabled]", true, @auto_assign.enabled? %><%=h l(:button_activate)%>
      </p>
      <div id="cr_auto_assign_settings" style="display: <%=  @auto_assign.enabled? ? 'true' : 'none'%>;">
        <p>
          <label><%=h l(:field_author)%></label>
          <%= select :auto_assign, :author_id,
            (@project.members.collect {|m| [m.user.name, m.user.id]}), :selected => @auto_assign.author_id, :required => true %>
        </p>
        <p>
          <label><%=h l(:label_candidates_of_reviewer)%></label>
          <% @project.users.each do |user| %>
            <label class="floating"><%= check_box_tag 'auto_assign[assignable_list][]', user.id, @auto_assign.assignable?(user) %> <%=h user %></label>
          <% end %>
        </p>
        <p>
          <label><%=h l(:field_subject)%></label>
          <%= text_field(:auto_assign, :subject, :size => 70) %>
        </p>
        <p>
          <label><%=h l(:field_description) %></label>
          <%= text_area :auto_assign, :description,
                   :cols => 30,
                   :rows => 12,
                   :accesskey => accesskey(:edit),
                   :class => 'wiki-edit' %>
        </p>
        <p>
        <div id="auto_assignment_filters">
          <%= render :partial => 'code_review_settings/filters' %>
        </div>
        </p>
      </div>
      <% if @old_reviews and @old_reviews.any? %>
        <hr/>
        <p>
          <%=h l(:old_review_data_found) %>
          <%=  radio_button_tag 'convert', true, :checked => false %><%=h l(:general_text_Yes) %>
          <%=  radio_button_tag 'convert', false, :checked => true %><%=h l(:general_text_No) %>
        </p>
      <% end %>
    </div>
    <%= submit_tag l(:button_update) %>
  <% end %>
</div>
<%= wikitoolbar_for 'auto_assign_description' %>

<%= observe_field 'auto_assign_enabled', {:function => "setAutoAssignSettingFormEnable(value);"} %>